package com.example.concurrentprogramming8016.future.completablefuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author liyanhao
 * @Date 2023-01-08 21:27
 * 两个线程任务相比较，有任何一个执行完成，就进行下一步操作，不关心运行结果
 */
public class RunAfterEitherDemo {
    public static void main(String[] args) {
        CompletableFuture.runAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("第一个阶段");
        }).runAfterEither(CompletableFuture.runAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("第二个阶段");

        }), () -> {
            System.out.println("最终计算完毕");
        }).join();
    }
}
